/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.service.ServiceAdminRemote;
import com.paris5.master2.guitarmaster.service.ServiceCartRemoteE;
import com.paris5.master2.guitarmaster.service.ServiceConvertorRemote;
import com.paris5.master2.guitarmaster.service.ServiceCustomerRemote;
import com.paris5.master2.guitarmaster.service.ServiceProductRemote;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Leticia
 */
public class EcomAction extends ActionSupport {

    private String message;
    private Properties properties = new Properties();

    private void initProperties() {
        properties.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.impl.SerialInitContextFactory");
        properties.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
        properties.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
        properties.setProperty("org.omg.CORBA.ORBInitialHost", "192.168.0.11");
        properties.setProperty("org.omg.CORBA.ORBInialPort", "3700");
    }

    public ServiceAdminRemote lookupServiceAdminRemote() {
        Context context;
        try {
            //initProperties();
            //context = new InitialContext(properties);
            context = new InitialContext();
            return (ServiceAdminRemote) context.lookup("admin");
        } catch (NamingException ex) {
            Logger.getLogger(EcomAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ServiceConvertorRemote lookupServiceCovertorRemote() {
        Context context;
        try {
//            initProperties();
//            context = new InitialContext(properties);
            context = new InitialContext();
            return (ServiceConvertorRemote) context.lookup("convert");
        } catch (NamingException ex) {
            Logger.getLogger(EcomAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ServiceProductRemote lookupServiceProductRemote() {
        Context context;
        try {
//            initProperties();
//            context = new InitialContext(properties);
            context = new InitialContext();
            return (ServiceProductRemote) context.lookup("product");
        } catch (NamingException ex) {
            Logger.getLogger(EcomAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ServiceCustomerRemote lookupServiceCustomerRemote() {
        Context context;
        try {
//            initProperties();
//            Context c = new InitialContext(properties);
            context = new InitialContext();
            return (ServiceCustomerRemote) context.lookup("customer");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public ServiceCartRemoteE lookupServiceCartERemote() {
        Context context;
        try {
//            initProperties();
//            Context c = new InitialContext(properties);
            context = new InitialContext();
            return (ServiceCartRemoteE) context.lookup("cartE");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public void saveSession(String key, Object object) {
        Map<String, Object> map = ActionContext.getContext().getSession();
        ActionContext.getContext().getSession().put(key, object);
    }

    public void removeSession(String key) {
        ActionContext.getContext().getSession().remove(key);
        Map<String, Object> map = ActionContext.getContext().getSession();
        System.out.println("");
    }

    public Object getFromSession(String key) {
        return ActionContext.getContext().getSession().get(key);
    }

    public void eraseSession() {
        removeSession("USER");
        removeSession("PANIER");
        removeSession("RATE");
        removeSession("TOTAL_PRICE");
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public void registerRate() {
        if (getFromSession("USER") != null)
        {
            if (getFromSession("RATE") == null)
            {
                try {
                    ServiceConvertorRemote serviceConvertorRemote = lookupServiceCovertorRemote();
                    saveSession("RATE", serviceConvertorRemote.getRate("EUR", "USD"));
                } catch (EcomException ex) {
                    Logger.getLogger(AvailableProducts.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }         
}
